from ninja import Router, Schema, Query
from django.db import connection
from django.core.paginator import Paginator
from utils.cj_response import ErrorResponse
from utils.sql_fet_list import dictfetchall

router = Router()


class PagesSchema(Schema):
    page: int = 1
    limit: int = 10


@router.get("pro/users/tree")
def pro_users(request, pages: PagesSchema = Query(...)):
    """
    获取前台用户
    :param request:
    :param pages: 分页参数
    :return:
    """
    if not pages.page or not pages.limit:
        return ErrorResponse(msg="分页参数不能为空")
    with connection.cursor() as cur:
        cur.execute('select * from users')
        result = dictfetchall(cur)
    paginator = Paginator(result, pages.limit)
    page_1 = paginator.get_page(pages.page)
    data_liat = []
    for i in page_1:
        del i['passStr']
        del i['password']
        data_liat.append(i)
    data = {
        "data_liat": data_liat,
        "total": paginator.count
    }
    return data


@router.get("back/users/all")
def back_users(request, pages: PagesSchema = Query(...)):
    """
    获取后台用户
    :param request:
    :param pages:
    :return:
    """
    if not pages.page or not pages.limit:
        return ErrorResponse(msg="分页参数不能为空")
    with connection.cursor() as cur:
        cur.execute('select * from system_user')
        result = dictfetchall(cur)
    paginator = Paginator(result, pages.limit)
    page_1 = paginator.get_page(pages.page)
    data_liat = []
    for i in page_1:
        del i['password']
        data_liat.append(i)
    data = {
        "data_liat": data_liat,
        "total": paginator.count
    }
    return data
